import vuex from "vuex";
import Vue from "vue";
import service from "../utils/service"

// 安装插件
Vue.use(vuex);

export default new vuex.Store({
    state:{
        count:1,
        age:16,
        isAdult:false,
        isAllowMarry:false,
        isLoading:false
    },
    getters:{
        isAdult:function(state){
            return state.isAdult?"已成年":"未成年";
        }
    },
    mutations:{
        // state：就是当前仓库的state，不用程序员传入
        // payload:载荷。载荷是提交mutation传的参数。
        mAddCount(state,payload){
            console.log("payload",payload);
            state.count+=payload;

        },
        mAddAge(state,payload){

            // 修改state中的数据。
            state.age += payload.inc;
            if(state.age>=18){
                state.isAdult = true;
            }else{
                state.isAdult = false;
            }
        },

        mSetIsLoading(state,payload){
            state.isLoading = payload.isLoading
        }

    },
    actions:{
        
        // context：就是store对象，不用程序员传入

        aAddAge(context,payload){
            console.log("payload",payload);
            service({
                url:"http://localhost:3000/inc"
            })
            .then(res=>{
                console.log("提交mutaton之前",context.state.age);
                // 提交mutation
                context.commit({
                    type:"mAddAge",
                    inc:res.data.count
                });
                console.log("提交mutaton之后",context.state.age);

            })
        }

    }

});